﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Swan.SwanWakeUp.AlarmActions
{
    public class TextToSpeechAction
    {
        private TextToSpeechOptions options;
        public readonly string Name = "Text to speech action";

        public TextToSpeechAction() 
        {
            options = new TextToSpeechOptions();
        }

        public void Speech(string msg)
        {
            System.Speech.Synthesis.SpeechSynthesizer s2 = new System.Speech.Synthesis.SpeechSynthesizer();
            //var voices = s2.GetInstalledVoices();
            //Random rand = new Random();
            //var voice =  voices[rand.Next(0, voices.Count - 2)];
            //voice.Enabled = true;
            //s2.SelectVoice("Microsoft Mike");

            //s2.SpeakCompleted += (o2, e2) => { System.Threading.Thread.CurrentThread.Interrupt(); };
            //s2.SpeakStarted += (o, e) => {System.Threading.Thread.CurrentThread.Join();};

            s2.Speak(msg);
        }

        [Serializable]
        public class TextToSpeechOptions : ISerializable
        {
            public string voice;
            public string Text;

            public TextToSpeechOptions(SerializationInfo info, StreamingContext context)
            {
                this.Voice = info.GetString("Voice");
                this.Text = info.GetString("Text");
            }

            public TextToSpeechOptions()
            {
            }

            public string Voice
            {
                get
                {
                    return this.voice;
                }
                set
                {
                    this.voice = value;
                }
            }

            public System.Collections.ObjectModel.ReadOnlyCollection<System.Speech.Synthesis.InstalledVoice> Voices
            {
                get
                {
                    System.Speech.Synthesis.SpeechSynthesizer ss = new System.Speech.Synthesis.SpeechSynthesizer();
                    return ss.GetInstalledVoices();
                }
            }

            

            public bool Save(string fileName)
            {
                //Saves the alarm to a textfile to be retrieved later
                FileStream fs = new FileStream(System.Windows.Application.Current.StartupUri.AbsolutePath + "\\" +  fileName, FileMode.OpenOrCreate);
                BinaryFormatter w = new BinaryFormatter();
                w.Serialize(fs, this);
                fs.Close();
                return true;
            }

            public static TextToSpeechOptions Open(string fileName)
            {
                FileStream fs = new FileStream(System.Windows.Application.Current.StartupUri.AbsolutePath + "\\" + fileName, FileMode.OpenOrCreate);
                BinaryFormatter bf = new BinaryFormatter();
                object ret = bf.Deserialize(fs);
                fs.Close();
                return ret as TextToSpeechOptions;
            }

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue("Voice", this.Voice);
                info.AddValue("Text", this.Text);
            }

            public void Save()
            {
                throw new NotImplementedException();
            }
        }

        public TextToSpeechOptions Options
        {
            get
            {
                return this.options;
            }
            set
            {
                this.options = value as TextToSpeechOptions;
            }
        }

        public void Init()
        {
            throw new NotImplementedException();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }
    }
}
